import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2022-12-21
 * Time: 2:55
 */
//熟悉jedis常用api
public class Test_API {
    static Jedis jedis=new Jedis("101.35.154.52",6379);
    private static void test_String() {
        jedis.set("k1","v1");
        jedis.set("k2","v2");
        jedis.set("k3","v3");
        jedis.set("k4","v4");
        jedis.set("k5","v5");
        Set<String> set = jedis.keys("*");
        //System.out.println(jedis.get("k1"));
    
        //使用iterator把它全部打印出来
        Iterator<String> iterator = set.iterator();
        for(set.iterator(); iterator.hasNext();){
            String k = iterator.next();
            System.out.println(k+"->"+jedis.get(k));
        }
        Boolean k2Exists=jedis.exists("k2");
        System.out.println("k2Exists="+k2Exists);
        System.out.println(jedis.ttl("k2"));
        
        jedis.mset("k6","v6","k7","v7");
        System.out.println(jedis.mget("k4","k6"));
        System.out.println("=====================");
    }
    
    private static void test_List(){
        jedis.lpush("l1","1","2","3","4","5");
        List<String> list1 = jedis.lrange("l1", 0, -1);
        for(String s:list1){
            System.out.println(s);
        }
        System.out.println("===================");
    
        String p1 = jedis.lpop("l1");
        System.out.println(p1);
        String p2 = jedis.rpop("l1");
        System.out.println(p2);
    }
    
    private static void test_Hash(){
        jedis.hset("h1","id","1");
        System.out.println(jedis.hget("h1","id"));
        HashMap<String,String> map=new HashMap<>();
        map.put("id","2");
        map.put("name","liu");
        map.put("sex","nan");
        map.put("address","jaingxi");
        jedis.hmset("h1",map);
        List<String> get = jedis.hmget("h1", "name", "sex");
        for(String s:get){
            System.out.println(s);
        }
    }
    private static  void test_Set(){
        jedis.sadd("s1","1");
        jedis.sadd("s1","2");
        jedis.sadd("s1","3");
        jedis.sadd("s1","4");
        jedis.sadd("s1","5");
        jedis.sadd("s1","6");
        Set<String> set = jedis.smembers("s1");
        for (String k : set) {
            System.out.println(k);
        }
        System.out.println(jedis.scard("s1"));
        jedis.srem("s1","1");
        System.out.println(jedis.scard("s1"));
    }
    private static void test_ZSet(){
        jedis.zadd("z1",30,"score1");
        jedis.zadd("z1",40,"score2");
        jedis.zadd("z1",50,"score3");
        jedis.zadd("z1",70,"score4");
        jedis.zadd("z1",20,"score5");
        Set<String> zset = jedis.zrange("z1", 0, -1);
        for (String s : zset) {
            System.out.println(s); //按照score的高低打印，从低到高
        }
    
    }
    public static void main(String[] args) {
        //test_String();
       // test_List();
        //test_Hash();
        //test_Set();
        test_ZSet();
    }
}
